package org.smr.ministore.micro;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.blade.dao.base.BaseMapper;
import org.blade.service.base.AbstractService;
import org.blade.utils.IdGenUtils;
import org.smr.ministore.dao.DictionaryMapper;
import org.smr.ministore.dao.DictionaryOrganMapper;
import org.smr.ministore.entities.Dictionary;
import org.smr.ministore.entities.DictionaryGroupQueryVO;
import org.smr.ministore.entities.DictionaryOrgan;
import org.smr.ministore.service.DictionaryService;

import org.blade.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Transactional(readOnly = true)
public class DictionaryMicroService extends AbstractService<Long, Dictionary> implements DictionaryService {

	@Autowired
	private DictionaryMapper mapper;

    @Autowired
    private DictionaryOrganMapper dictionaryOrganMapper;

    public DictionaryMicroService(DictionaryMapper mapper) {
        super(mapper);
    }

    @ResponseBody
    @Transactional(readOnly = false)
    @RequestMapping(method = RequestMethod.POST, value = "/Dictionary/batchSave")
	public int batchSave(@RequestBody List<Dictionary> entities) {

	    this.batchSaveAttatch(entities);
    	return mapper.batchInsert(entities);
    }

    @ResponseBody
    @Transactional(readOnly = false)
    @RequestMapping(method = RequestMethod.PUT, value = "/Dictionary/batchModify")
    public int batchModify(List<Dictionary> entities) {
	    this.batchModifyAttatch(entities);
        return  mapper.batchUpdate(entities);
    }

    @ResponseBody
    @Transactional(readOnly = false)
    @RequestMapping(method = RequestMethod.PUT, value = "/Dictionary/save")
    public int save(@RequestBody Dictionary entity) {
	    this.saveAttatch(entity);

	    int effectCount = 0;
        effectCount += mapper.insert(entity);
        DictionaryOrgan dictionaryOrgan = new DictionaryOrgan();
        IdGenUtils.saveAttatch(dictionaryOrgan);
        dictionaryOrgan.setBelongToOrganId(entity.getOrganId());
        dictionaryOrgan.setDictionaryId(entity.getId());
        dictionaryOrgan.setOrganPath(entity.getOrganPath());
        dictionaryOrgan.setOrganId(entity.getOrganId());

        effectCount += dictionaryOrganMapper.insert(dictionaryOrgan);
        return effectCount;
    }

    @ResponseBody
    @Transactional(readOnly = false)
    @RequestMapping(method = RequestMethod.PUT, value = "/Dictionary/updateById")
    public int updateById(@RequestBody Dictionary id) {
	    this.modifyAttach(id);
   	 	return mapper.updateById(id);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/Dictionary/queryById/{id}")
    public Dictionary queryById( @PathVariable("id")Long id ) {
    	return (Dictionary) mapper.selectById(id);
    }

    @ResponseBody
    @Transactional(readOnly = false)
    @RequestMapping(method = RequestMethod.DELETE, value = "/Dictionary/deleteById/{id}")
    public int deleteById( @PathVariable("id")Long id ) {
        return mapper.deleteById(id);
    }

    @Override
    public int deleteByIdLogical(Long id) {
        return 0;
    }

    @ResponseBody
    @RequestMapping( method = RequestMethod.POST, value = "/Dictionary/paginate/{pageSize}/{pageNum}")
    public Pager<Dictionary> paginate(@RequestBody Dictionary entity, @PathVariable("pageSize")Long pageSize,  @PathVariable("pageNum")Long pageNum ) {
        Pager<Dictionary> pager = new Pager(pageSize, pageNum);
        List<Dictionary> list = this.mapper.pagination( entity, pager );
        pager.setEntities(list);
        return pager;
    }


    @ResponseBody
    @RequestMapping( method = RequestMethod.POST, value = "/Dictionary/getDictionaryGroup")
    public List<Dictionary> getDictionaryGroup(@RequestBody DictionaryGroupQueryVO query) {

        int sys = query.getSys();
        String bigType = query.getBigType();
        Long organId = query.getOrganId();
        String organPath = query.getOrganPath();

        return this.mapper.getDictionaryGroup(sys,bigType,organId,organPath);

    }


    @ResponseBody
    @RequestMapping( method = RequestMethod.POST, value = "/Dictionary/getReferencedDictionaryList")
    public List<Map<String, Object>> getReferencedDictionaryList(@RequestBody Map<String,Object> params ) {

        int sys = MapUtils.getIntValue(params,"sys");
        String bigType = MapUtils.getString(params,"bigType");
        Long organId = MapUtils.getLong(params,"organId");
        Long originId = MapUtils.getLong(params,"originId");

        return this.mapper.getReferencedDictionaryList(sys,organId,bigType,originId);
    }




    @ResponseBody
    @RequestMapping( method = RequestMethod.POST, value = "/Dictionary/getDictionaryGroup/{sys}/{bigType}/{organId}/{organPath}")
    public List<Dictionary> getDictionaryGroup(@PathVariable("sys") int sys,
                                               @PathVariable("bigType") String bigType,
                                               @PathVariable("organId") Long organId,
                                               @PathVariable("organPath") String organPath) {

        return this.mapper.getDictionaryGroup(sys,bigType,organId,organPath);
    }

    @ResponseBody
    @RequestMapping( method = RequestMethod.POST, value = "/Dictionary/getOrganReferencedDictionaryPagination/{pageSize}/{pageNum}")
    public  Pager<Dictionary> getOrganReferencedDictionaryPagination(@RequestBody Dictionary entity,
                                                                     @PathVariable("pageSize")Long pageSize,  @PathVariable("pageNum")Long pageNum) {
        Pager<Dictionary> pager = new Pager( pageSize, pageNum );
        List list  = mapper.getOrganReferencedDictionaryPagination(entity, pager);
        pager.setEntities(list);
        return pager;
    }

    @ResponseBody
    @RequestMapping( method = RequestMethod.POST, value = "/Dictionary/getOrganReferencedDictionaryPagination")
    public List<Dictionary> getOrganReferencedDictionary(Dictionary entity) {
        return this.mapper.getOrganReferencedDictionary(entity);
    }

    @Override
    public Long getId() {
        return IdGenUtils.getNextLongId();
    }
}
